package controller;

import com.google.gson.Gson;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import pojo.Blog;
import pojo.Follow;
import pojo.Image;
import pojo.User;
import service.BlogService;
import service.FollowService;
import utils.util;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/follow")
public class FollowController {

    @Resource
    private FollowService followService;

    @RequestMapping("/addFollow")
    public String addBlog(@RequestParam Map<String, String> map) throws NoSuchAlgorithmException {
        String strFollow = map.get("strFollow");//获取安卓端传过来的键
        Gson gson = new Gson();
        Follow follow = gson.fromJson(strFollow, Follow.class);
        follow.setIsDel(0);
        follow.setTime(util.getNowTime());
        int i = followService.insert(follow);
        if (i > 0) {
            String JsonStr = new Gson().toJson(follow);
            return JsonStr;
        } else {
            return "fail";     //保存失败
        }
    }

    @RequestMapping("/deletFollow")
    public void deletFollow(@RequestParam Map<String, String> map) throws NoSuchAlgorithmException {
        String foID = map.get("foID");//获取安卓端传过来的键
        int followid=Integer.parseInt(foID);
        System.out.println("foID"+followid);
        followService.delete(followid);
    }

    @RequestMapping("/getFollowByUid")
    public void getFollowByUid(@RequestParam Map<String, String> map, HttpServletResponse response) throws IOException {
        String beSubscribe = map.get("beSubscribe");
        String subscribe = map.get("subscribe");
        Follow follow=followService.getFollowByUid(beSubscribe,subscribe);
        response.setContentType("charset=UTF-8");
        if (follow!=null){
            int followid=follow.getFollowid();
            String fid=String.valueOf(followid);
            response.getWriter().write(fid);
        }else {
            response.getWriter().write("follow");
        }
    }

    @RequestMapping("/judgeFlo")
    public void judgeFlo(@RequestParam Map<String,String> map, HttpServletResponse response) throws IOException {
        String beSubscribe = map.get("beSubscribe");
        String subscribe = map.get("subscribe");
        Follow follow = followService.getFollowByUid(subscribe,beSubscribe);
        if (follow != null) {  //如果存在则取消关注
            int followid = follow.getFollowid();
            followService.delete(followid);
            response.getWriter().write("delet");
        } else {

            followService.follow(subscribe,beSubscribe);
            response.getWriter().write("success");
        }
    }
    @RequestMapping("/judgeStu")
    public void judgeStu(@RequestParam Map<String,String> map,HttpServletResponse response) throws IOException {
        String beSubscribe = map.get("beSubscribe");
        String subscribe = map.get("subscribe");
        Follow follow = followService.getFollowByUid(subscribe,beSubscribe);
        if(follow!=null){
            response.getWriter().write("exist");//存在，则显示已关注
        }else {
            response.getWriter().write("delet");
        }
    }

//粉丝数查询
    @RequestMapping("getFollownumBybeSubscribe")
    public String getFollownumBybeSubscribe(@RequestParam Map<String, String> map,HttpServletResponse response) throws IOException {
        String beSubscribe=map.get("beSubscribe");
        Gson gson = new Gson();
        Follow follow= gson.fromJson(beSubscribe, Follow.class);
        String userid = follow.getBeSubscribe();
        int fansNums = followService.getFollownumBybeSubscribe(userid);

        String JsonStr = new Gson().toJson(fansNums);
        System.out.println(fansNums);
        return JsonStr;
    }
//关注数查询
    @RequestMapping("getFollownumBysubscribe")
    public String getFollownumBysubscribe(@RequestParam Map<String, String> map,HttpServletResponse response) throws IOException {
        String subscribe=map.get("subscribe");
        Gson gson = new Gson();
        Follow follow= gson.fromJson(subscribe, Follow.class);
        String userid = follow.getSubscribe();
        int FollowNum = followService.getFollownumBysubscribe(userid);

        String JsonStr = new Gson().toJson(FollowNum);
        System.out.println(FollowNum);
        return JsonStr;
    }

}

